<html>
  <head>
    <title>Loading the Bing Maps map using PHP and SOAP Services</title>
  </head>
  <body>
  <form action="BingMaps_SOAP_GeocodeSample.php" method="post">
    Bing Maps Key: <input type="text" name="key" value="<?php echo (isset($_POST['key'])?$_POST['key']:'') ?>"><br>
    Address: <input type="text" name="address" value="<?php echo (isset($_POST['address'])?$_POST['address']:'') ?>"><br>
    Zoom Level (1-19): <input type="text" name="zoom" value="<?php echo (isset($_POST['zoom'])?$_POST['zoom']:'') ?>"><br>
    <input type="submit" value="Submit">
  </form>
<?php
  if(isset($_POST['key']) && isset($_POST['address']) && isset($_POST['zoom']))
  {
    // Using local versions of WSDL files
    $geocodeServiceWsdl = "http://dev.virtualearth.net/webservices/v1/metadata/geocodeservice/geocodeservice.wsdl";
    $imageryServiceWsdl = "http://dev.virtualearth.net/webservices/v1/metadata/imageryservice/imageryservice.wsdl";

    $credentials = array('ApplicationId' => 'AjyC0pJX36cF2dV8X3LUxtFaYb9hNfmprGxQDxSAyLSxHRPSGI-bkL2oO5zIhUfa');

    // Start geocode
    echo "<p>Starting geocode...</p>";

    // Create a SOAP client for the Bing Maps SOAP Geocode service using its WSDL
    $geocodeClient = new SoapClient($geocodeServiceWsdl, array('trace' => 1));
        
    // Construct a request "object" 
    $geocodeRequest = array(
      'Credentials' => $credentials,
      'Query' => $_POST['address']
    );

    // Send the Geocode request to the web service and receive a response
    try {
      $geocodeResponse = $geocodeClient->Geocode(array('request' => $geocodeRequest));
    }
    catch(SoapFault $e)
    {
      die('Fault occurred using Web Service: '.$e->getMessage());
    }
    
    // Ouput geocoded latitude and longitude of provided address
    if (is_array($geocodeResponse->GeocodeResult->Results->GeocodeResult)) {
      if (is_array($geocodeResponse->GeocodeResult->Results->GeocodeResult[0]->Locations->GeocodeLocation)) {
        $lat = $geocodeResponse->GeocodeResult->Results->GeocodeResult[0]->Locations->GeocodeLocation[0]->Latitude;
        $lon = $geocodeResponse->GeocodeResult->Results->GeocodeResult[0]->Locations->GeocodeLocation[0]->Longitude;
      }
      else {
        $lat = $geocodeResponse->GeocodeResult->Results->GeocodeResult[0]->Locations->GeocodeLocation->Latitude;
        $lon = $geocodeResponse->GeocodeResult->Results->GeocodeResult[0]->Locations->GeocodeLocation->Longitude;
      }
    }
    else
    {
      $lat = $geocodeResponse->GeocodeResult->Results->GeocodeResult->Locations->GeocodeLocation[0]->Latitude;
      $lon = $geocodeResponse->GeocodeResult->Results->GeocodeResult->Locations->GeocodeLocation[0]->Longitude;
    }
    echo "<p>Latitude: <b>" . $lat . "</b></p>";
    echo "<p>Longitude: <b>" . $lon . "</b></p>";

    // Display the geocoded point on the map
    echo "<p>Starting get image...</p>";
  
    // Create a SOAP client for the Imagery service
    $imageryClient = new SoapClient($imageryServiceWsdl, array('trace' => 1));
  
    // Create a location 'object' to use as a parameter, using geocoded lat/lon
    $location = array(
      'Latitude' => $lat,
      'Longitude' => $lon
    );
  
    // Create options 'object' including zoom level and pushpin to use
    $options = array(
      'ZoomLevel' => $_POST['zoom']
    );
    
    // Create a pushpin 'object'
    $pushpin = array(
      'IconStyle' => "11",
      'Location' => $location
    );
    
    // Create an array of pushpins to use as a parameter
    $pushpins = array($pushpin);
        
    // Construct the request 'object' for the Imagery service
    $mapUriRequest = array(
      'Credentials' => $credentials,
      'Pushpins' => $pushpins,
      'Options' => $options
    );

    // Send the Imagery service request and get the URI of a static map image as a response
    try {
      $mapUriResponse = $imageryClient->GetMapUri(array('request' => $mapUriRequest));
    }
    catch(SoapFault $e)
    {
        die('Fault occurred using Web Service: '.$e->getMessage());
    }

    // Display the resulting image in the browser
    echo "<img src='" . $mapUriResponse->GetMapUriResult->Uri . "'><br>";
  }
  else
  {
    echo "<p>Please enter your Bing Maps key, Address, and a Zoom Level from 1-19, then click Submit.</p>";
  }
?>
  </body>
</html>
